<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" v-if="ids==''">
            <a-form-item  label="客户名称" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
            <a-tree-select
              style="width:100%"
              :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
              :treeData="treeData"
              v-model="model.customerId"
              placeholder="请选择客户名称"
              @select="loadTree"
              :disabled="disableSubmit">
            </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="试用时长(天)" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="trialDuration">
              <a-input-number v-model="model.trialDuration" placeholder="请输入试用时长(天)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="激活时长(年)" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activationDuration">
              <a-input-number v-model="model.activationDuration" placeholder="请输入激活时长(年)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="安卓费用" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="androidCost">
              <a-input-number v-model="model.androidCost" placeholder="请输入安卓费用" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="IOS费用" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="iosCost">
              <a-input-number v-model="model.iosCost" placeholder="请输入IOS费用" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!--<a-col :span="24">
            <a-form-model-item label="是否删除(0未删除 1已删除)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deleted">
              <a-input-number v-model="model.deleted" placeholder="请输入是否删除(0未删除 1已删除)" style="width: 100%" />
            </a-form-model-item>
          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import {queryKHTreeList } from '@/api/api'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'DeviceConfigForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          customerId:'',
          trialDuration:'',
          activationDuration:'',
          androidCost:'',
          iosCost:''
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        disableSubmit:false,
        confirmLoading: false,
        ids:'',
        validatorRules: {
          trialDuration: [
            { required: true,  message: '请输入试用时长(天)', trigger: 'blur' }
          ],
          activationDuration: [
            { required: true,  message: '请输入激活时长(年)', trigger: 'blur' }
          ],
          androidCost: [
            { required: true,  message: '请输入Android费用', trigger: 'blur' }
          ],
          iosCost: [
            { required: true,  message: '请输入IOS费用', trigger: 'blur' }
          ],
        },
        treeData:[],
        url: {
          add: "/device/deviceActivation/batchConfig",
          edit: "/device/deviceActivation/edit",
          queryById: "/device/deviceActivation/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        debugger
        if(record.type!=undefined){
          this.ids=record.ids;
          this.model.ids=record.ids;
        }else{
          this.model = Object.assign({}, record);
        }

        this.visible = true;
        this.loadTree();
        this.getConfigInfo();
      },
      getConfigInfo() {
        var that=this;
        this.$http.get('/device/deviceConfig/quertyConfigInfo')
          .then(res => {
            debugger
            if(res.result!=null){
              //this.model = res.result
              var data=res.result;
              that.model.trialDuration=data.trialDuration
              that.model.activationDuration=data.activationDuration
              that.model.androidCost=data.androidCost
              that.model.iosCost=data.iosCost

            }

          })
      },
      loadTree(){
        var that = this;
        queryKHTreeList().then((res)=>{
          if(res.success){
            console.log('----queryKHTreeList---')
            console.log(res)
            that.treeData = [];
            let treeList = res.result.treeList
            for(let a=0;a<treeList.length;a++){
              let temp = treeList[a];
              // temp.isLeaf = temp.leaf;
              that.treeData.push(temp);
            }
          }
        });
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>